import sys
import os
import time
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *

class Demo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        layout = QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop)

        toolBar=QToolBar()

        cb=QCheckBox('TextVisible')
        cb.stateChanged.connect(self.OnTextVisibleChanged)
        toolBar.addWidget(cb)

        cb=QCheckBox('InvertedAppearance')
        cb.stateChanged.connect(self.OnInvertedAppearanceChanged)
        toolBar.addWidget(cb)

        toolBar.addWidget(QLabel('Alignment:'))
        cb=QComboBox()
        cb.addItems(Qt.AlignmentFlag.__members__.keys())
        cb.currentTextChanged.connect(self.OnAlignmentChanged)
        toolBar.addWidget(cb)

        toolBar.addWidget(QLabel('Orientation:'))
        cb=QComboBox()
        cb.addItems(Qt.Orientation.__members__.keys())
        cb.currentTextChanged.connect(self.OnOrientationChanged)
        toolBar.addWidget(cb)

        toolBar.addWidget(QLabel('TextDirection:'))
        cb=QComboBox()
        cb.addItems(QProgressBar.Direction.__members__.keys())
        cb.currentTextChanged.connect(self.OnDirectionChanged)
        toolBar.addWidget(cb)

        toolBar.addWidget(QLabel('Format:'))
        self.format=QLineEdit()
        self.format.editingFinished.connect(self.OnFormatChanged)
        toolBar.addWidget(self.format)
        
        toolBar.addWidget(QLabel('Max:'))
        self.max=QLineEdit()
        self.max.editingFinished.connect(self.OnMaxChanged)        
        toolBar.addWidget(self.max)

        btn=QPushButton('Start')
        btn.clicked.connect(self.OnStart)
        toolBar.addWidget(btn)

        layout.addWidget(toolBar)

        self.progressBar=QProgressBar()
        layout.addWidget(self.progressBar)

        self.setLayout(layout)
    def OnTextVisibleChanged(self,state):
        self.progressBar.setTextVisible(state)
        self.parent.OnShowMessage(f'Set TextVisible:{state}')
    def OnInvertedAppearanceChanged(self,state):
        self.progressBar.setInvertedAppearance(state)
        self.parent.OnShowMessage(f'Set InvertedAppearance:{state}')
    def OnAlignmentChanged(self,text):
        alignment=Qt.AlignmentFlag[text]
        self.progressBar.setAlignment(alignment)
        self.parent.OnShowMessage(f'Set Alignment:{alignment}')
    def OnOrientationChanged(self,text):
        orientation=Qt.Orientation[text]
        self.progressBar.setOrientation(orientation)
        self.parent.OnShowMessage(f'Set Orientation:{orientation}')
    def OnDirectionChanged(self,text):
        direction=QProgressBar.Direction[text]
        self.progressBar.setTextDirection(direction)
        self.parent.OnShowMessage(f'Set TextDirection:{direction}')
    def OnFormatChanged(self):
        format=self.format.text()
        self.progressBar.setFormat(format)
        self.parent.OnShowMessage(f'Set Format:{format}')
    def OnMaxChanged(self):        
        max=int(self.max.text())
        self.progressBar.setRange(0,max)
        self.parent.OnShowMessage(f'Set Maximum:{max}')
    def OnStart(self):
        for i in range(0,self.progressBar.maximum()+1):
            self.progressBar.setValue(i)
            time.sleep(50/1000)
def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet